﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using QLTT.Models;
using QLTT.Commands;
using System.Windows.Input;

namespace QLTT.ViewModels
{
    class AllTeacherViewModel:WorkspaceViewModel
    {
        private ObservableCollection<TeacherViewModel> _lstTeacherViewModel;
        private icanDataContext _dataContext;
        private DelegateCommand _findCommand;
        private DelegateCommand _addCommand;
        private DelegateCommand _deleteCommand;
        private DelegateCommand _showClassCommand;
        private IQueryable<TeacherViewModel> _query;
        private string _teacherName;
        public AllTeacherViewModel(icanDataContext dataContext)
        {
            _lstTeacherViewModel = new ObservableCollection<TeacherViewModel>();
            _dataContext = dataContext;
            base.DisplayName = Properties.Resources.Base_DisplayName_AllTeacher;

        }
        public AllTeacherViewModel(icanDataContext dataContext, bool isGetAll)
        {
            _lstTeacherViewModel = new ObservableCollection<TeacherViewModel>();
            _dataContext = dataContext;
            if (isGetAll)
            {
                _query = from teacher in _dataContext.Teachers orderby teacher.ID descending select new TeacherViewModel(_dataContext, teacher);
                this.NumRow = _query.Count();
                this.CurPage = 1;
                this.UpdateList();
            }
            base.DisplayName = Properties.Resources.Base_DisplayName_AllTeacher;
        }
        public string Name{get;set;}
        //{
        //    get { return _teacherName; }
        //    set { _teacherName = value; }
        //}
        
        public ObservableCollection<TeacherViewModel> AllTeachers
        {
            get { return _lstTeacherViewModel; }
            set {
                _lstTeacherViewModel = value;
                base.OnPropertyChanged("AllTeachers");
            }
        }
        public ICommand FindCommand
        {
            get
            {
                if (_findCommand == null)
                {
                    _findCommand = new DelegateCommand(Find);
                }
                return _findCommand;
            }
        }
        public ICommand AddCommand
        {
            get
            {
                if (_addCommand == null)
                {
                    _addCommand = new DelegateCommand(AddTeacher);
                }
                return _addCommand;
            }
        }
        public ICommand DeleteCommand
        {
            get
            {
                if (_deleteCommand == null)
                {
                    _deleteCommand = new DelegateCommand(DeleteTeacher, CanDeleteTeacher);
                }
                return _deleteCommand;
            }
        }
        public ICommand ShowClassCommand
        {
            get
            {
                if (_showClassCommand == null)
                {
                    _showClassCommand = new DelegateCommand(ShowClass, CanShowClass);
                }
                return _showClassCommand;
            }
        }
        
        public void Find()
        {
            IQueryable<Teacher> myQuery = _dataContext.Teachers;
            if (!string.IsNullOrEmpty(this.Name))
                myQuery = myQuery.Where<Teacher>(teacher => teacher.Name.Contains(this.Name));
                       
            //Order
            myQuery = myQuery.OrderByDescending(t => t.ID);
            //Select
            _query = myQuery.Select(teacher => new TeacherViewModel(_dataContext, teacher));
            this.NumRow = _query.Count();
            //Reset to page 0
            this.CurPage = 1;
            this.UpdateList();
        }
        
        public void AddTeacher()
        {
            TeacherViewModel ws = new TeacherViewModel(_dataContext);
            Mediator.NotifyColleagues<WorkspaceViewModel>(Messages.WorkspaceAdd, ws);
        }
        
        private void DeleteTeacher()
        {
            foreach (TeacherViewModel tvm in AllTeachers)
            {
                if (tvm.IsSelected)
                {
                    tvm.Delete();
                }
            }
            this.NumRow = _query.Count();
            this.UpdateList();

        }
        private bool CanDeleteTeacher()
        {
            if (ViewModelBase.CurrentRole != UserRole.Administrator)
                return false;
            TeacherViewModel tVM = AllTeachers.FirstOrDefault(t => t.IsSelected == true);
            if (tVM == null)
                return false;
            return tVM.CanDelete;
        }
        public void ShowClass()
        {
            TeacherViewModel tVM = this.AllTeachers.FirstOrDefault(t => t.IsSelected == true);
            if (tVM == null)
                return;
            ClassScheduleViewModel cVM = new ClassScheduleViewModel(_dataContext, false);
            cVM.Teacher = tVM.ID;
            cVM.Find();
            Mediator.NotifyColleagues<WorkspaceViewModel>(Messages.WorkspaceAdd, cVM);
        }
        public bool CanShowClass()
        {
            return this.AllTeachers.Where(t => t.IsSelected == true).Count() > 0;
        }
        [MediatorLib.MediatorMessageSink(Messages.ListTeacherUpdated, ParameterType = typeof(Teacher))]
        public void UpdateListTeacher(Teacher teacher)
        {
            this.NumRow = _query.Count();
            this.UpdateList();
        }

        public override void UpdateList()
        {
            List<TeacherViewModel> lstTeachers = _query.Skip((this.CurPage - 1) * this.RowsPerPage).Take(this.RowsPerPage).ToList();
            
            AllTeachers = new ObservableCollection<TeacherViewModel>(lstTeachers);
            base.UpdateList();
        }
    }
}
